using System;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Configuration;
using System.Web.UI.WebControls;
using System.Security;
using System.Security.Principal;
using System.IO;
using System.Data;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;

namespace Starznet.SharePoint.SPDAT.ApplicationPages.Administration
{
    public class LogFileListPage : StarznetBaseAdministrationPage
    {
        protected SPGridView GvFiles;
        private MenuTemplate _oFileMenu;
        //private string _sLogLocation = SPUtility.GetGenericSetupPath("LOGS");
        private string _sLogLocation = string.Empty;
        private List<FileInfo> oFiles;
      
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            _sLogLocation = SPDiagnosticsService.Local.LogLocation;

            GvFiles.Columns.Clear();

            _oFileMenu = new MenuTemplate();
            _oFileMenu.ID = "FileMenu";

            SPMenuField oMenu = new SPMenuField();
            oMenu.HeaderText = "File Name";
            oMenu.TextFields = "Name";
            oMenu.MenuTemplateId = "FileMenu";
            oMenu.NavigateUrlFields = "Name";
            oMenu.NavigateUrlFormat = "LogFileView.aspx?Name={0}";
            oMenu.TokenNameAndValueFields = "NAME=Name";
            oMenu.SortExpression = "Name";

            MenuItemTemplate oView = new MenuItemTemplate(this.GetGlobalResourceObject("szadm", "LogList_MenuView").ToString(), "/_layouts/images/EDITITEM.GIF");
            oView.ClientOnClickNavigateUrl = "LogFileView.aspx?Name=%NAME%";
            _oFileMenu.Controls.Add(oView);

            this.Page.Controls.Add(_oFileMenu);
            GvFiles.Columns.Add(oMenu);

            BoundField oDateCreated = new BoundField();
            oDateCreated.DataField = "CreationTime";
            oDateCreated.HeaderText = this.GetGlobalResourceObject("szadm", "LogList_ColDateCreated").ToString();
            oDateCreated.SortExpression = "CreationTime";
            GvFiles.Columns.Add(oDateCreated);

            //if (IsPostBack) return;

            oFiles = new List<FileInfo>();
            FileInfo oInfo;
            foreach (string sFile in Directory.GetFiles(_sLogLocation))
            {
                oInfo = new FileInfo(sFile);
                //Dont add the technology configuration wizard file logs
                if (!oInfo.Name.ToUpper().StartsWith("PSCDIAGNOSTICS") && !oInfo.Name.ToUpper().StartsWith("UPGRADE"))
                    oFiles.Add(new FileInfo(sFile));
            }
            GvFiles.DataSource = oFiles;
            GvFiles.DataBind();
        }
    }
}
